iT邦幫忙

2022 iThome 鐵人賽

DAY 11
0
Modern Web

React應用記錄誌系列 第 11

Day 11 實作練習 Redux

  • 分享至 

  • xImage
  •  

https://ithelp.ithome.com.tw/upload/images/20220926/20139800CYOYdoPuJa.png

Index.js

import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import { Provider } from 'react-redux';
import store from './store';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
     <Provider  store={store}>
        <App />
     </Provider>      
  </React.StrictMode>
);

Store.js

import {createStore} from 'redux'
import rootReducer from './reducer/index'
const store = createStore(rootReducer);
export default store;

reducer/index.js
combineReducers=>將多個reducer合併

import todoReducer from "./todoReducer";
import { combineReducers } from "redux";
const rootReducer = combineReducers({
    todoReducer
})
export default rootReducer;

todoReducer.js

const initialData = {
    list:[]
}
const todoReducer = (state = initialData, action) => {
    switch(action.type){
        case "ADDTODO":
            const { id,data} = action.payload;
            return{
                ...state, list:[
                    ...state.list,
                    {
                     id:id,
                     data:data
                    }
               ]
            }
            case "CLEARTODO":                  
                return{
                    ...state,
                    list:[]
                }

        default:return state;        
    }  
}
export default todoReducer;

Action.js

import {v4 as uuidv4} from "uuid";
export const addTodo =(data)=>{
    return{
        type:"ADDTODO",
        payload:{
            id: uuidv4(),
            data:data
        }
    }
}
export const cleardTodo =(data)=>{
    return{
        type:"CLEARTODO",
        
    }

Todo.js

import React ,{ useState }from 'react'
import { useSelector,useDispatch } from 'react-redux';
import {addTodo,cleardTodo } from '../action/index';

const Todo = () => {
    const [input,setInput] = useState("");
    const list = useSelector((state)=>state.todoReducer.list)
    const dispatch = useDispatch();
       
  return (
    <div className="container">
    <div className="app-box">
        <div className="header">Add Todoitem Here</div>
        <input 
        className="line-input" 
        type="text" 
        placeholder='Add TodoItems...'
        value={input}
        onChange={(e)=>setInput(e.target.value)} />
        
        <i 
        className='fa fa-plus add-btn' 
        onClick={()=>dispatch(addTodo(input),
        setInput(''))}></i>

         <div >
           { list.map((e)=>{
              return(
                <div className='list-item'key={e.id}>
                  <h3>{e.data}</h3>
                </div>
              )
              
            })
          }
         </div>
         <div>
          <button 
          className='button-delete'
          onClick={()=>dispatch(cleardTodo())}>Clear All</button>
         </div>

     </div>
   </div>
  )
}
export default Todo


上一篇
Day 10 Redux介紹
下一篇
Day12 實作練習Axios
系列文
React應用記錄誌31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言